<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserAssessmentAnswer extends Model
{
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id', 'question_id', 'selected_option', 'score_earned'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'created_at' => 'datetime',
    ];

    /**
     * 所属用户关系
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * 所属问题关系
     */
    public function question()
    {
        return $this->belongsTo(AssessmentQuestion::class, 'question_id');
    }

    /**
     * 获取答案文本
     */
    public function getAnswerTextAttribute()
    {
        if (!$this->question) {
            return '未知选项';
        }

        $options = $this->question->options;
        return $options[$this->selected_option] ?? '未知选项';
    }

    /**
     * 获取分数百分比
     */
    public function getScorePercentageAttribute()
    {
        // 假设最高分是5分
        return round(($this->score_earned / 5) * 100, 1);
    }
}